# coding=utf-8
# 一键安装python脚本

import os
import sys
if os.getuid() == 0:
	pass
else:
	print '当前用户不是root用户，请以root用户执行脚本'
	sys.exit(1)

version = raw_input('请输入你想安装的python版本(2.7或3.7)')
if version == '2.7':
	url = 'https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tgz'
elif version == '3.7':
	url = 'https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz'
else:
	print '你输入的版本号有误，请输入2.7或3.7'
	sys.exit(1)

cmd = 'wget ' +url
res = os.system(cmd)
if res != 0:
	print '下载源码包失败，请检查网络'
	sys.exit(1)


if version == '2.7':
	package_name = 'Python-2.7.16'
else:
	package_name = 'Python-3.7.4'

cmd = 'tar -xvf'+package_name+'.tgz'
res = os.system(cmd)
if res !=0:
	os.system('rm'+package_name+'.tgz')
	print '解压源码包失败，请重新运行该脚本'
	sys.exit(1)

#path = 'mkdir -p /usr/local/python'
#res = os.system(path)
yum = 'yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel gcc make'
#res = os.system(yum)

cmd = 'cd '+package_name+'&& ./configure --prefix=/usr/local/python && make && make install'
res = os.system(cmd)
if res != 0:
	print '编译python源码失败，请检查是否缺少依赖库'
	sys.exit(1)

#创建python3软链接
ln -s /usr/local/python/bin/pip3  /usr/bin/pip3
ln -s /usr/local/python/bin/python3 /usr/bin/python3




